﻿using System;
using System.Web;
using System.Web.Security;


namespace GUI.Pages.VietID
{
    public partial class DoLogin : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Response.ContentType = "text/html";

                var username = "";
                if (Request.QueryString["username"] != null && Request.QueryString["username"] != String.Empty)
                {

                    username = Request.QueryString["username"].Trim();
                }
                var email = "";
                if (Request.QueryString["email"] != null && Request.QueryString["email"] != String.Empty)
                {
                    email = Request.QueryString["email"].Trim();
                }
                var fullName = "";
                if (Request.QueryString["full_name"] != null && Request.QueryString["full_name"] != String.Empty)
                {
                    fullName = Request.QueryString["full_name"].Trim();
                }
                var avatar = "";
                if (Request.QueryString["avatar"] != null && Request.QueryString["avatar"] != String.Empty)
                {
                    avatar = Request.QueryString["avatar"].Trim();
                }

                var id = "";
                if (Request.QueryString["id"] != null && Request.QueryString["id"] != String.Empty)
                {
                    id = Request.QueryString["id"].Trim();
                }


                if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(email))
                {
                    if (HttpContext.Current.Request.IsAuthenticated)
                    {
                        var ident = HttpContext.Current.User.Identity as FormsIdentity;

                        if (ident != null)
                        {
                            try
                            {
                                var ticket1 = ident.Ticket;
                                var userData = ticket1.UserData.Split('|');
                                var uName = userData[1];
                                if (uName != username)
                                {
                                    var userDataString = id + "|" + username + "|" + email + "|" + fullName + "|" + avatar;

                                    var authCookie = FormsAuthentication.GetAuthCookie(username, true);
                                    var ticket = FormsAuthentication.Decrypt(authCookie.Value);
                                    var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, userDataString);
                                    authCookie.Value = FormsAuthentication.Encrypt(newTicket);
                                    authCookie.Expires = DateTime.Now.AddYears(1);
                                    Response.Cookies.Add(authCookie);

                                    Response.Write("{'id':'" + id + "', 'username':'" + username + "'}");
                                }
                                else
                                {
                                    Response.Write("");
                                }
                            }
                            catch (Exception)
                            {
                                Response.Write("");
                            }
                        }
                    }
                    else
                    {
                        var userDataString = id + "|" + username + "|" + email + "|" + fullName + "|" + avatar;

                        var authCookie = FormsAuthentication.GetAuthCookie(username, true);
                        var ticket = FormsAuthentication.Decrypt(authCookie.Value);
                        var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, userDataString);
                        authCookie.Value = FormsAuthentication.Encrypt(newTicket);
                        authCookie.Expires = DateTime.Now.AddYears(1);
                        Response.Cookies.Add(authCookie);

                        Response.Write("{'id':'" + id + "', 'username':'" + username + "'}");
                    }
                }

            }
        }
    }
}